package com.eugene.shiftscalendar;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by Eugene on 21.03.14.
 */
public class DateDialog extends Activity {
    public static Date date;
    private boolean holiday;
    private CheckBox check;
    private RadioButton btn1;
    private RadioButton btn2;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.datedialog);
        String month[] = {"января","февраля","марта","апреля","мая","июня","июля","августа","сентября","октября","ноября","декабря",""};
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date);
        this.setTitle(cal2.get(Calendar.DAY_OF_MONTH) + " " + month[cal2.get(Calendar.MONTH)] + " " + cal2.get(Calendar.YEAR));

        int info = DateInfo.getDateInfo(date, getApplicationContext());
        check = (CheckBox) this.findViewById(R.id.checkBox);
        btn1 = (RadioButton) findViewById(R.id.radioButton);
        btn2 = (RadioButton) findViewById(R.id.radioButton2);
        check.setChecked(info != -1);
        btn1.setEnabled(info != -1);
        btn2.setEnabled(info != -1);

        DateType type = new DateType();
        holiday = type.isHoliday(cal2, getApplicationContext());

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        btn1.setChecked(holiday);
        btn2.setChecked(!holiday);

        check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn1.setEnabled(check.isChecked());
                btn2.setEnabled(check.isChecked());
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                if(!check.isChecked()){
                    DateInfo.setDateInfo(date, -1, getApplicationContext());
                }
                else{
                    DateInfo.setDateInfo(date, holiday ? 1 : 0, getApplicationContext());
                }
            }
        });

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn1.setChecked(true);
                btn2.setChecked(false);
                DateInfo.setDateInfo(date, 1, getApplicationContext());
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn2.setChecked(true);
                btn1.setChecked(false);
                DateInfo.setDateInfo(date, 0, getApplicationContext());
            }
        });

        Button ok = (Button) findViewById(R.id.ok_button);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateParent();
                finish();
            }
        });
    }

    void updateParent(){
        Intent intent = new Intent(this, MainWindow.class);
        intent.setAction(MainWindow.ACTION_UPDATE);
// Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
// since it seems the onUpdate() is only fired on that:
        int[] ids = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), MainWindow.class));
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
        sendBroadcast(intent);
    }
}
